package com.fingard.dsp.bank.directbank.ccb12;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 实时代收付保费
 * 参考文档：新一代代理保险保险公司交易接口_P5与保险公司（发布版）_V6.1.xlsx
 * 章节：
 * 申请实时代收保费	P5381915G
 * 确认实时代收保费	P5381915H
 */
public class CCB12RealTime extends CCB12Base implements IRealTime {
    @Override
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9108.singleDetail.recAct + "]批量商户配置信息";
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }

            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            txCode = "P5381915G";
            reqSeqID = p_req9108.singleDetail.askNumber;
            writeCommonHeader(xmlWriter, actItem);

            xmlWriter.writeStartElement("APP_ENTITY");
            xmlWriter.writeElementString("AgInsPyFBsnSbdvsn_Cd", "10");//代理保险缴费业务细分代码(10-缴纳保费 14-续期缴费)
            xmlWriter.writeElementString("Ins_BillNo", p_req9108.singleDetail.insuPolicyNum);//投保单号
            xmlWriter.writeElementString("InsPolcy_No", p_req9108.singleDetail.sourceNoteCode);//保单号码
            xmlWriter.writeElementString("Plchd_Nm", p_req9108.singleDetail.payName);//投保人名称
            xmlWriter.writeElementString("Plchd_Crdt_TpCd", convertCertTypeToBank(p_req9108.singleDetail.certType));//投保人证件类型代码
            xmlWriter.writeElementString("Plchd_Crdt_No", p_req9108.singleDetail.certNum);//投保人证件号码
            xmlWriter.writeElementString("Ins_PyF_Amt", p_req9108.singleDetail.payAmount);//保险缴费金额
            xmlWriter.writeElementString("InsPrem_PyMd_Cd", "2");//保费支付方式代码(目前仅支持 2-借记卡)
            xmlWriter.writeElementString("Cst_AccNo", p_req9108.singleDetail.payAct);//客户账户
            xmlWriter.writeEndDocument();

            xmlWriter.writeEndDocument();
            String tmpStrRet = sendToBank(xmlWriter.toXmlString());
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
            boolean isContinue = false;
            if (tmpBkReader != null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpBkReader.getFirstText("/TX/TX_HEADER/SYS_RESP_DESC");
                String respCode = tmpBkReader.getFirstText("/TX/TX_HEADER/SYS_RESP_CODE");
                if ("000000000000".equals(respCode)) {
                    String phoneNumber = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/APP_ENTITY/Plchd_Move_TelNo");
                    String acctNo = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/APP_ENTITY/Cst_AccNo");
                    if (StringHelper.hasAnyChar(phoneNumber) && StringHelper.hasAnyChar(acctNo)) {
                        isContinue = true;
                    }
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSbMsg.toString();
            }

            if (isContinue) {
                XmlTextWriter xmlWriter1 = new XmlTextWriter();
                txCode = "P5381915H";
                reqSeqID = p_req9108.singleDetail.askNumber;
                writeCommonHeader(xmlWriter1, actItem);

                xmlWriter1.writeStartElement("APP_ENTITY");
                xmlWriter1.writeElementString("Orig_TxnSrlNo", p_req9108.singleDetail.askNumber);//#原交易流水号
                xmlWriter1.writeElementString("SMS_Vld_CD", p_req9108.singleDetail.smsCode);//#短信验证码
                xmlWriter1.writeElementString("Plchd_Nm", p_req9108.singleDetail.payName);//投保人名称
                xmlWriter1.writeElementString("Plchd_Crdt_TpCd", convertCertTypeToBank(p_req9108.singleDetail.certType));//投保人证件类型代码
                xmlWriter1.writeElementString("Plchd_Crdt_No", p_req9108.singleDetail.certNum);//投保人证件号码
                xmlWriter.writeEndDocument();

                tmpStrRet = sendToBank(xmlWriter.toXmlString());
                tmpSbMsg = new StringBuilder();
                tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
                if (tmpBkReader != null) {
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.respInfo = tmpBkReader.getFirstText("/TX/TX_HEADER/SYS_RESP_DESC");
                    String respCode = tmpBkReader.getFirstText("/TX/TX_HEADER/SYS_RESP_CODE");
                    if ("000000000000".equals(respCode)) {
                        tmpRetDetail.transState = PaymentState.SUCCESS;
                        tmpRetDetail.payInfoCode = respCode;
                    } else {
                        tmpRetDetail.transState = PaymentState.FAIL;
                        tmpRetDetail.payInfoCode = respCode;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        return null;
    }
}
